package com.weetop.cfw.home_page.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.coorchice.library.SuperTextView
import com.google.android.material.tabs.TabLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.SearchResultPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.SearchResultView
import com.weetop.cfw.bean.GeneralCodeTableBean
import com.weetop.cfw.bean.KindBean
import com.weetop.cfw.bean.SiteBean
import com.weetop.cfw.bean.WorkshopWarehouseSearchResultBean
import com.weetop.cfw.constants.Constants
import com.weetop.cfw.home_page.adapter.WorkshopWarehouseSearchResultAdapter
import com.weetop.cfw.kind.activity.WorkshopDetailActivity
import com.weetop.cfw.utils.LogInUtils
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import kotlinx.android.synthetic.main.activity_search_result.*

/**
 * 搜索结果界面
 */
class SearchResultActivity : CommonBaseActivity(), View.OnClickListener, SearchResultView,
    OnRefreshLoadMoreListener, TextView.OnEditorActionListener {

    private lateinit var workshopWarehouseSearchResultAdapter: WorkshopWarehouseSearchResultAdapter

    private val workshopWarehouseList = ArrayList<WorkshopWarehouseSearchResultBean.DataBean>()

    private lateinit var areaFilterPopupWindow: PopupWindow//区域
    private lateinit var mianJiFilterPopupWindow: PopupWindow//面积
    private lateinit var priceFilterPopupWindow: PopupWindow//价格
    private lateinit var industryFilterPopupWindow: PopupWindow//行业
    private lateinit var sourceFilterPopupWindow: PopupWindow//来源

    private lateinit var filterTagFlowLayout1: TagFlowLayout
    private lateinit var filterTagFlowLayout2: TagFlowLayout
    private lateinit var filterTagFlowLayout3: TagFlowLayout
    private lateinit var filterTagFlowLayout4: TagFlowLayout
    private lateinit var filterTagFlowLayout5: TagFlowLayout

    private lateinit var filterPopupWindowView1: View
    private lateinit var filterPopupWindowView2: View
    private lateinit var filterPopupWindowView3: View
    private lateinit var filterPopupWindowView4: View
    private lateinit var filterPopupWindowView5: View

    private var intId: String = "1"//意向ID，出租为1，出售为2
    private var typeId: String = "1201"//类型ID，厂库为1201，仓库为1202
    private var areaId: String = "0"//区域ID
    private var acrId: String = "0"//面积ID
    private var pucateId: String = "0"//价格区间
    private var vocId: String = "0"//行业ID
    private var utype: String = "0"//来源ID
    private var sort: String = "0"//默认排序
    private var currentPageNumber: Int = 1//当前数据的页数
    private var totalCount: Int = 0//数据的总记录数

    private var key: String = ""//搜索关键词，默认为""

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否加载更多


    private lateinit var searchResultPresenterImp: SearchResultPresenterImp

    companion object {

        private const val SEARCH_KEY = "SEARCH_KEY"

        fun startSearchResultActivity(context: Context, searchKey: String) {
            val intent = Intent(context, SearchResultActivity::class.java)
            intent.putExtra(SEARCH_KEY, searchKey)
            ActivityUtils.startActivity(intent)
        }

        fun startSearchResultActivity(context: Context, searchKey: String, index: Int) {
            val intent = Intent(context, SearchResultActivity::class.java)
            intent.putExtra(SEARCH_KEY, searchKey)
            intent.putExtra("index", index)
            ActivityUtils.startActivity(intent)
        }
    }

    override fun getLayoutId() = R.layout.activity_search_result


    private var index = 0
    override fun initView(savedInstanceState: Bundle?) {
        searchResultPresenterImp = SearchResultPresenterImp()
        searchResultPresenterImp.attachView(this)

        index = intent.getIntExtra("index", 0)

        searchResultSmartRefreshLayout.setOnRefreshLoadMoreListener(this)
        searchResultSmartRefreshLayout.setEnableAutoLoadMore(false)
        setViewsOnClickListener(
            this,
            linearArea,
            linearPrice,
            linearSource,
            linearMianJi,
            imageSearchLogo,
            linearIndustry,
            imageSearchResultBack
        )
        editSearchContent.setOnEditorActionListener(this)

        filterPopupWindowView1 =
            View.inflate(context, R.layout.layout_filter_popup_window_layout, null)
        filterPopupWindowView2 =
            View.inflate(context, R.layout.layout_filter_popup_window_layout, null)
        filterPopupWindowView3 =
            View.inflate(context, R.layout.layout_filter_popup_window_layout, null)
        filterPopupWindowView4 =
            View.inflate(context, R.layout.layout_filter_popup_window_layout, null)
        filterPopupWindowView5 =
            View.inflate(context, R.layout.layout_filter_popup_window_layout, null)
        filterTagFlowLayout1 = filterPopupWindowView1.findViewById(R.id.filterTagFlowLayout)
        filterTagFlowLayout2 = filterPopupWindowView2.findViewById(R.id.filterTagFlowLayout)
        filterTagFlowLayout3 = filterPopupWindowView3.findViewById(R.id.filterTagFlowLayout)
        filterTagFlowLayout4 = filterPopupWindowView4.findViewById(R.id.filterTagFlowLayout)
        filterTagFlowLayout5 = filterPopupWindowView5.findViewById(R.id.filterTagFlowLayout)

        initAreaFilterPopupWindow()
        initIndustryFilterPopupWindow()
        initMianJiFilterPopupWindow()
        initPriceFilterPopupWindow()
        initSourceFilterPopupWindow()

        priceFilterPopupWindow.setOnDismissListener {
            textPrice.setTextColor(ColorUtils.getColor(android.R.color.black))
            if (!areaFilterPopupWindow.isShowing && !industryFilterPopupWindow.isShowing && !mianJiFilterPopupWindow.isShowing && !sourceFilterPopupWindow.isShowing) {
                imageVagueBg.visibility = View.INVISIBLE
            }
            imagePrice.setImageResource(R.mipmap.arrow_down_black_small)
        }
        industryFilterPopupWindow.setOnDismissListener {
            textIndustry.setTextColor(ColorUtils.getColor(android.R.color.black))
            if (!areaFilterPopupWindow.isShowing && !mianJiFilterPopupWindow.isShowing && !priceFilterPopupWindow.isShowing && !sourceFilterPopupWindow.isShowing) {
                imageVagueBg.visibility = View.INVISIBLE
            }
            imageIndustry.setImageResource(R.mipmap.arrow_down_black_small)
        }
        areaFilterPopupWindow.setOnDismissListener {
            textArea.setTextColor(ColorUtils.getColor(android.R.color.black))
            if (!industryFilterPopupWindow.isShowing && !priceFilterPopupWindow.isShowing && !mianJiFilterPopupWindow.isShowing && !sourceFilterPopupWindow.isShowing) {
                imageVagueBg.visibility = View.INVISIBLE
            }
            imageArea.setImageResource(R.mipmap.arrow_down_black_small)
        }
        mianJiFilterPopupWindow.setOnDismissListener {
            textMianJi.setTextColor(ColorUtils.getColor(android.R.color.black))
            if (!industryFilterPopupWindow.isShowing && !priceFilterPopupWindow.isShowing && !areaFilterPopupWindow.isShowing && !sourceFilterPopupWindow.isShowing) {
                imageVagueBg.visibility = View.INVISIBLE
            }
            imageMianJi.setImageResource(R.mipmap.arrow_down_black_small)
        }
        sourceFilterPopupWindow.setOnDismissListener {
            textSource.setTextColor(ColorUtils.getColor(android.R.color.black))
            if (!industryFilterPopupWindow.isShowing && !priceFilterPopupWindow.isShowing && !areaFilterPopupWindow.isShowing && !mianJiFilterPopupWindow.isShowing) {
                imageVagueBg.visibility = View.INVISIBLE
            }
            imageSource.setImageResource(R.mipmap.arrow_down_black_small)
        }

        initTabLayout()
    }

    override fun initData(savedInstanceState: Bundle?) {
        workshopWarehouseSearchResultAdapter = WorkshopWarehouseSearchResultAdapter(
            R.layout.layout_item_workshop_browse_record,
            workshopWarehouseList
        )
        workshopWarehouseSearchResultAdapter.emptyView = emptyView
        workshopWarehouseSearchResultAdapter.setOnItemClickListener { _, _, position ->
//            if(LogInUtils.userIsLogIn()){
            when (searchResultMagicIndicator.selectedTabPosition) {
                0 -> {//厂房出租
//                        if(LogInUtils.userIsLogIn()){
                    WorkshopDetailActivity.startWorkshopDetailActivity(
                        this,
                        workshopWarehouseList[position].infoid,
                        2
                    )
//                        }
                }
                1 -> {//厂房出售
//                        if(LogInUtils.userIsLogIn()){
                    WorkshopDetailActivity.startWorkshopDetailActivity(
                        this,
                        workshopWarehouseList[position].infoid,
                        1
                    )
//                        }
                }
                2 -> {//仓库出租
//                        if(LogInUtils.userIsLogIn()){
                    WorkshopDetailActivity.startWorkshopDetailActivity(
                        this,
                        workshopWarehouseList[position].infoid,
                        4
                    )
//                        }
                }
                3 -> {//仓库出售
//                        if(LogInUtils.userIsLogIn()){
                    WorkshopDetailActivity.startWorkshopDetailActivity(
                        this,
                        workshopWarehouseList[position].infoid,
                        3
                    )
//                        }
                }
//                }
            }
        }
        searchResultRV.adapter = workshopWarehouseSearchResultAdapter

        key =
            if (intent.getStringExtra(SEARCH_KEY) == null) "" else intent.getStringExtra(SEARCH_KEY)!!

        LogUtils.d("key_search", key)

        editSearchContent.setText(key)

        searchResultPresenterImp.getAreaData(this)
        searchResultPresenterImp.getIndustryData(this)
        searchResultPresenterImp.getPriceData(this)
        searchResultPresenterImp.getSourceData(this)
        searchResultPresenterImp.getQuYuData(this)

        val queryParamMap = mapOf(
            "siteid" to Constants.siteId.toString(),
            "intid" to intId,
            "typeid" to typeId,
            "areaid" to areaId,
            "acrid" to acrId,
            "pucate" to pucateId,
            "vocid" to vocId,
            "utype" to utype,
            "sort" to sort,
            "key" to key,
            "curpage" to currentPageNumber.toString(),
            "pagesize" to "10"
        )
        searchResultPresenterImp.getSearchResultData(this, queryParamMap)
    }

    private fun initTabLayout() {
        searchResultMagicIndicator.addTab(searchResultMagicIndicator.newTab().setText("厂房出租"))
        searchResultMagicIndicator.addTab(searchResultMagicIndicator.newTab().setText("厂房出售"))
        searchResultMagicIndicator.addTab(searchResultMagicIndicator.newTab().setText("仓库出租"))
        searchResultMagicIndicator.addTab(searchResultMagicIndicator.newTab().setText("仓库出售"))

        searchResultMagicIndicator.addOnTabSelectedListener(object :
            TabLayout.OnTabSelectedListener {

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                if (tab != null) {
                    intId =
                        if (searchResultMagicIndicator.selectedTabPosition == 0 || searchResultMagicIndicator.selectedTabPosition == 2) {
                            "1"
                        } else {
                            "2"
                        }
                    typeId =
                        if (searchResultMagicIndicator.selectedTabPosition == 0 || searchResultMagicIndicator.selectedTabPosition == 1) {
                            "1201"
                        } else {
                            "1202"
                        }
                    searchResultSmartRefreshLayout.autoRefresh()
                }
            }

        })

        intId =
            if (searchResultMagicIndicator.selectedTabPosition == 0 || searchResultMagicIndicator.selectedTabPosition == 2) {
                "1"
            } else {
                "2"
            }
        typeId =
            if (searchResultMagicIndicator.selectedTabPosition == 0 || searchResultMagicIndicator.selectedTabPosition == 1) {
                "1201"
            } else {
                "1202"
            }
        searchResultMagicIndicator.selectTab(searchResultMagicIndicator.getTabAt(index))
    }

    /**
     * 初始化价格筛选的PopupWindow
     */
    private fun initPriceFilterPopupWindow() {
        priceFilterPopupWindow = PopupWindow(context)
        priceFilterPopupWindow.contentView = filterPopupWindowView1
        priceFilterPopupWindow.isOutsideTouchable = false
        priceFilterPopupWindow.setBackgroundDrawable(
            ContextCompat.getDrawable(
                context,
                R.drawable.solid_white_shape
            )
        )
        priceFilterPopupWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        priceFilterPopupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
    }

    /**
     * 初始化来源筛选的PopupWindow
     */
    private fun initSourceFilterPopupWindow() {
        sourceFilterPopupWindow = PopupWindow(context)
        sourceFilterPopupWindow.contentView = filterPopupWindowView5
        sourceFilterPopupWindow.isOutsideTouchable = false
        sourceFilterPopupWindow.setBackgroundDrawable(
            ContextCompat.getDrawable(
                context,
                R.drawable.solid_white_shape
            )
        )
        sourceFilterPopupWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        sourceFilterPopupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
    }

    /**
     * 初始化行业筛选的PopupWindow
     */
    private fun initIndustryFilterPopupWindow() {
        industryFilterPopupWindow = PopupWindow(context)
        industryFilterPopupWindow.contentView = filterPopupWindowView2
        industryFilterPopupWindow.isOutsideTouchable = false
        industryFilterPopupWindow.setBackgroundDrawable(
            ContextCompat.getDrawable(
                context,
                R.drawable.solid_white_shape
            )
        )
        industryFilterPopupWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        industryFilterPopupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
    }

    /**
     * 初始化地区筛选的PopupWindow
     */
    private fun initAreaFilterPopupWindow() {
        areaFilterPopupWindow = PopupWindow(context)
        areaFilterPopupWindow.contentView = filterPopupWindowView3
        areaFilterPopupWindow.isOutsideTouchable = false
        areaFilterPopupWindow.setBackgroundDrawable(
            ContextCompat.getDrawable(
                context,
                R.drawable.solid_white_shape
            )
        )
        areaFilterPopupWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        areaFilterPopupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
    }

    /**
     * 初始化面积筛选的PopupWindow
     */
    private fun initMianJiFilterPopupWindow() {
        mianJiFilterPopupWindow = PopupWindow(context)
        mianJiFilterPopupWindow.contentView = filterPopupWindowView4
        mianJiFilterPopupWindow.isOutsideTouchable = false
        mianJiFilterPopupWindow.setBackgroundDrawable(
            ContextCompat.getDrawable(
                context,
                R.drawable.solid_white_shape
            )
        )
        mianJiFilterPopupWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        mianJiFilterPopupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
    }

    override fun onDestroy() {
        super.onDestroy()
        Constants.workshopAreaId = 0
        Constants.workshopIntId = 0
        searchResultPresenterImp.detachView()
    }


    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.imageSearchResultBack -> ActivityUtils.finishActivity(this)
            R.id.linearIndustry -> {
                if (industryFilterPopupWindow.isShowing) {
                    industryFilterPopupWindow.dismiss()
                } else {
                    industryFilterPopupWindow.showAsDropDown(linearFilterSearchResult)
                    imageVagueBg.visibility = View.VISIBLE
                    priceFilterPopupWindow.dismiss()
                    areaFilterPopupWindow.dismiss()
                    mianJiFilterPopupWindow.dismiss()
                    sourceFilterPopupWindow.dismiss()

                    textIndustry.setTextColor(ColorUtils.getColor(R.color.c_1381CC))
                    imageIndustry.setImageResource(R.mipmap.arrow_down_blue_small)
                }
            }
            R.id.linearPrice -> {
                if (priceFilterPopupWindow.isShowing) {
                    priceFilterPopupWindow.dismiss()
                } else {

                    priceFilterPopupWindow.showAsDropDown(linearFilterSearchResult)
                    imageVagueBg.visibility = View.VISIBLE
                    areaFilterPopupWindow.dismiss()
                    mianJiFilterPopupWindow.dismiss()
                    industryFilterPopupWindow.dismiss()
                    sourceFilterPopupWindow.dismiss()

                    textPrice.setTextColor(ColorUtils.getColor(R.color.c_1381CC))
                    imagePrice.setImageResource(R.mipmap.arrow_down_blue_small)
                }
            }
            R.id.linearArea -> {
                if (!areaFilterPopupWindow.isShowing) {
                    areaFilterPopupWindow.showAsDropDown(linearFilterSearchResult)
                    imageVagueBg.visibility = View.VISIBLE
                    priceFilterPopupWindow.dismiss()
                    industryFilterPopupWindow.dismiss()
                    mianJiFilterPopupWindow.dismiss()
                    sourceFilterPopupWindow.dismiss()

                    textArea.setTextColor(ColorUtils.getColor(R.color.c_1381CC))
                    imageArea.setImageResource(R.mipmap.arrow_down_blue_small)
                } else {
                    areaFilterPopupWindow.dismiss()
                }
            }
            R.id.linearMianJi -> {
                if (!mianJiFilterPopupWindow.isShowing) {
                    mianJiFilterPopupWindow.showAsDropDown(linearFilterSearchResult)
                    imageVagueBg.visibility = View.VISIBLE
                    priceFilterPopupWindow.dismiss()
                    industryFilterPopupWindow.dismiss()
                    areaFilterPopupWindow.dismiss()
                    sourceFilterPopupWindow.dismiss()

                    textMianJi.setTextColor(ColorUtils.getColor(R.color.c_1381CC))
                    imageMianJi.setImageResource(R.mipmap.arrow_down_blue_small)
                } else {
                    mianJiFilterPopupWindow.dismiss()
                }
            }
            R.id.linearSource -> {
                if (sourceFilterPopupWindow.isShowing) {
                    sourceFilterPopupWindow.dismiss()
                } else {
                    sourceFilterPopupWindow.showAsDropDown(linearFilterSearchResult)
                    imageVagueBg.visibility = View.VISIBLE
                    priceFilterPopupWindow.dismiss()
                    industryFilterPopupWindow.dismiss()
                    areaFilterPopupWindow.dismiss()
                    mianJiFilterPopupWindow.dismiss()

                    textSource.setTextColor(ColorUtils.getColor(R.color.c_1381CC))
                    imageSource.setImageResource(R.mipmap.arrow_down_blue_small)
                }
            }
            R.id.imageSearchLogo -> beginToSearch()
        }
    }

    /**
     * 种类数据获取成功
     */
    override fun kindDataGetSuccess(kindBean: KindBean?) {

    }

    /**
     * 面积数据获取成功
     */
    override fun areaDataGetSuccess(generalCodeTableBean: GeneralCodeTableBean?) {
        if (generalCodeTableBean != null) {
            val areaDataList = generalCodeTableBean.data
            filterTagFlowLayout4.adapter =
                object : TagAdapter<GeneralCodeTableBean.DataBean>(areaDataList) {
                    override fun getView(
                        parent: FlowLayout?,
                        position: Int,
                        t: GeneralCodeTableBean.DataBean?
                    ): View {
                        val filterSearchResultFlowLayoutView = LayoutInflater.from(context)
                            .inflate(R.layout.layout_item_filter_tag_flow, parent, false)
                        val textFilterItem =
                            filterSearchResultFlowLayoutView.findViewById<SuperTextView>(R.id.textFilterItem)
                        textFilterItem.text = areaDataList[position].codename
                        return filterSearchResultFlowLayoutView
                    }

                    override fun onSelected(position: Int, view: View?) {
                        super.onSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(true, view)
                    }

                    override fun unSelected(position: Int, view: View?) {
                        super.unSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(false, view)
                        acrId = "0"
                    }
                }

            filterTagFlowLayout4.setOnTagClickListener { view, position, parent ->
                acrId = areaDataList[position].codeid.toString()
                searchResultSmartRefreshLayout.autoRefresh()
                mianJiFilterPopupWindow.dismiss()
                true
            }
        }
    }


    /**
     * 区域数据获取成功
     */
    override fun quYuGetSuccess(siteBean: SiteBean?) {
        if (siteBean != null) {
            val siteDataBeanList = siteBean.data
            filterTagFlowLayout3.adapter =
                object : TagAdapter<SiteBean.DataBean>(siteDataBeanList) {
                    override fun getView(
                        parent: FlowLayout?,
                        position: Int,
                        t: SiteBean.DataBean?
                    ): View {
                        val filterSearchResultFlowLayoutView = LayoutInflater.from(context)
                            .inflate(R.layout.layout_item_filter_tag_flow, parent, false)
                        val textFilterItem =
                            filterSearchResultFlowLayoutView.findViewById<SuperTextView>(R.id.textFilterItem)
                        textFilterItem.text = siteDataBeanList[position].areaname
                        return filterSearchResultFlowLayoutView
                    }

                    override fun onSelected(position: Int, view: View?) {
                        super.onSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(true, view)
                    }

                    override fun unSelected(position: Int, view: View?) {
                        super.unSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(false, view)
                        areaId = "0"
                    }
                }

            filterTagFlowLayout3.setOnTagClickListener { view, position, parent ->
                areaId = siteDataBeanList[position].areaid.toString()
                Constants.workshopAreaId = siteDataBeanList[position].areaid
                searchResultSmartRefreshLayout.autoRefresh()
                areaFilterPopupWindow.dismiss()
                true
            }
        }
    }

    /**
     * 切换选中状态和非选中状态
     */
    private fun selectedStateAndUnselectedStateSwitch(isSelected: Boolean, view: View?) {
        if (view != null) {
            val textFilterItem = view.findViewById<SuperTextView>(R.id.textFilterItem)
            if (isSelected) {
                textFilterItem.setTextColor(ColorUtils.getColor(android.R.color.white))
                textFilterItem.solid = ColorUtils.getColor(R.color.c_1381CC)
            } else {
                textFilterItem.setTextColor(ColorUtils.getColor(android.R.color.black))
                textFilterItem.solid = ColorUtils.getColor(R.color.F5F8FC)
            }
        }
    }

    /**
     * 价格筛选数据获取成功
     */
    override fun priceFilterDataGetSuccess(generalCodeTableBean: GeneralCodeTableBean?) {
        if (generalCodeTableBean != null) {
            val priceFilterDataList = generalCodeTableBean.data
            filterTagFlowLayout1.adapter =
                object : TagAdapter<GeneralCodeTableBean.DataBean>(priceFilterDataList) {
                    override fun getView(
                        parent: FlowLayout?,
                        position: Int,
                        t: GeneralCodeTableBean.DataBean?
                    ): View {
                        val filterSearchResultFlowLayoutView = LayoutInflater.from(context)
                            .inflate(R.layout.layout_item_filter_tag_flow, parent, false)
                        val textFilterItem =
                            filterSearchResultFlowLayoutView.findViewById<SuperTextView>(R.id.textFilterItem)
                        textFilterItem.text = priceFilterDataList[position].codename
                        return filterSearchResultFlowLayoutView
                    }

                    override fun onSelected(position: Int, view: View?) {
                        super.onSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(true, view)
                    }

                    override fun unSelected(position: Int, view: View?) {
                        super.unSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(false, view)
                        pucateId = "0"
                    }

                }

            filterTagFlowLayout1.setOnTagClickListener { view, position, parent ->
                pucateId = priceFilterDataList[position].codeid.toString()
                searchResultSmartRefreshLayout.autoRefresh()
                priceFilterPopupWindow.dismiss()
                true
            }
        }
    }

    /**
     * 行业数据获取成功
     */
    override fun industryFilterDataGetSuccess(generalCodeTableBean: GeneralCodeTableBean?) {
        if (generalCodeTableBean != null) {
            val industryFilterDataList = generalCodeTableBean.data
            filterTagFlowLayout2.adapter =
                object : TagAdapter<GeneralCodeTableBean.DataBean>(industryFilterDataList) {
                    override fun getView(
                        parent: FlowLayout?,
                        position: Int,
                        t: GeneralCodeTableBean.DataBean?
                    ): View {
                        val filterSearchResultFlowLayoutView = LayoutInflater.from(context)
                            .inflate(R.layout.layout_item_filter_tag_flow, parent, false)
                        val textFilterItem =
                            filterSearchResultFlowLayoutView.findViewById<SuperTextView>(R.id.textFilterItem)
                        textFilterItem.text = industryFilterDataList[position].codename
                        return filterSearchResultFlowLayoutView
                    }

                    override fun onSelected(position: Int, view: View?) {
                        super.onSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(true, view)
                    }

                    override fun unSelected(position: Int, view: View?) {
                        super.unSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(false, view)
                        vocId = "0"
                    }

                }

            filterTagFlowLayout2.setOnTagClickListener { view, position, parent ->
                vocId = industryFilterDataList[position].codeid.toString()
                searchResultSmartRefreshLayout.autoRefresh()
                industryFilterPopupWindow.dismiss()
                true
            }
        }
    }

    /**
     * 来源数据获取成功
     */
    override fun sourceFilterDataGetSuccess(generalCodeTableBean: GeneralCodeTableBean?) {
        if (generalCodeTableBean != null) {
            val sourceFilterDataList = generalCodeTableBean.data
            filterTagFlowLayout5.adapter =
                object : TagAdapter<GeneralCodeTableBean.DataBean>(sourceFilterDataList) {
                    override fun getView(
                        parent: FlowLayout?,
                        position: Int,
                        t: GeneralCodeTableBean.DataBean?
                    ): View {
                        val filterSearchResultFlowLayoutView = LayoutInflater.from(context)
                            .inflate(R.layout.layout_item_filter_tag_flow, parent, false)
                        val textFilterItem =
                            filterSearchResultFlowLayoutView.findViewById<SuperTextView>(R.id.textFilterItem)
                        textFilterItem.text = sourceFilterDataList[position].codename
                        return filterSearchResultFlowLayoutView
                    }

                    override fun onSelected(position: Int, view: View?) {
                        super.onSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(true, view)
                    }

                    override fun unSelected(position: Int, view: View?) {
                        super.unSelected(position, view)
                        selectedStateAndUnselectedStateSwitch(false, view)
                        utype = "0"
                    }

                }
            filterTagFlowLayout5.setOnTagClickListener { view, position, parent ->
                utype = sourceFilterDataList[position].codeid.toString()
                searchResultSmartRefreshLayout.autoRefresh()
                sourceFilterPopupWindow.dismiss()
                true
            }

        }
    }

    /**
     * 搜索结果获取成功
     */
    override fun searchResultGetSuccess(workshopWarehouseSearchResultBean: WorkshopWarehouseSearchResultBean?) {
        if (workshopWarehouseSearchResultBean != null) {
            totalCount = workshopWarehouseSearchResultBean.records
            if (isRefresh) {
                workshopWarehouseList.clear()
                workshopWarehouseList.addAll(workshopWarehouseSearchResultBean.data)
                workshopWarehouseSearchResultAdapter.notifyDataSetChanged()
                searchResultSmartRefreshLayout.finishRefresh(true)
                if (workshopWarehouseSearchResultBean.data.size > 0) {
                    currentPageNumber = 1
                }
                return
            }
            if (isLoadMore) {
                workshopWarehouseList.addAll(workshopWarehouseSearchResultBean.data)
                workshopWarehouseSearchResultAdapter.notifyDataSetChanged()
                searchResultSmartRefreshLayout.finishLoadMore(true)
                if (workshopWarehouseSearchResultBean.data.size > 0) {
                    currentPageNumber += 1
                }
                isLoadMore = false
                return
            }
            workshopWarehouseList.addAll(workshopWarehouseSearchResultBean.data)
            workshopWarehouseSearchResultAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 搜索结果获取失败
     */
    override fun searchResultGetFailed() {
        searchResultSmartRefreshLayout.finishRefresh(false)
        searchResultSmartRefreshLayout.finishLoadMore(false)
    }

    /**
     * 进行搜索
     */
    override fun beginToSearch() {
        key = editSearchContent.text.toString().trim()
        searchResultSmartRefreshLayout.autoRefresh()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if (totalCount <= workshopWarehouseList.size) {
            showNativeShortToast("暂无更多数据")
            searchResultSmartRefreshLayout.finishLoadMoreWithNoMoreData()
        } else {
            isLoadMore = true
            val tempQueryMap = mapOf(
                "siteid" to Constants.siteId.toString(),
                "intid" to intId,
                "typeid" to typeId,
                "areaid" to areaId,
                "acrid" to acrId,
                "pucate" to pucateId,
                "vocid" to vocId,
                "utype" to utype,
                "sort" to sort,
                "key" to key,
                "curpage" to ((currentPageNumber + 1).toString()),
                "pagesize" to "10"
            )
            searchResultPresenterImp.getSearchResultData(this, tempQueryMap)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        val tempQueryMap = mapOf(
            "siteid" to Constants.siteId.toString(),
            "intid" to intId,
            "typeid" to typeId,
            "areaid" to areaId,
            "acrid" to acrId,
            "pucate" to pucateId,
            "vocid" to vocId,
            "utype" to utype,
            "sort" to sort,
            "key" to key,
            "curpage" to "1",
            "pagesize" to "10"
        )
        searchResultPresenterImp.getSearchResultData(this, tempQueryMap)
    }

    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            beginToSearch()
            return true
        }
        return false
    }

}
